package net.cgsoft.widget.adapter;

import android.content.Context;
import android.graphics.Color;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;


import net.cgsoft.widget.R;
import net.cgsoft.widget.model.DateModel;
import net.cgsoft.widget.model.ScheduleDate;
import net.cgsoft.widget.utils.Util;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;

public class CalendarAdapter extends BaseAdapter {

    private static final String TAG = "CalendarAdapter";
    private final int mWhite, mBlack, mTransparent;
    String mCurrentDate, mCurrentMonth;
    Context mContext;
    private int mCount;
    ArrayList<DateModel> mDateModelArrays;
    ArrayList<ScheduleDate> mDateList;
    ArrayList<String> mDateString;

    public CalendarAdapter(Context context, Calendar calendar, ArrayList<ScheduleDate> dateList) {
        this.mContext = context;
        mDateList = dateList == null ? new ArrayList<ScheduleDate>() : dateList;
        mWhite = mContext.getResources().getColor(R.color.white);
        mBlack = mContext.getResources().getColor(R.color.black);
        mTransparent = mContext.getResources().getColor(R.color.transparent);
        mCurrentDate = Util.mDateFormat.format(calendar.getTime());
        mCurrentMonth = Util.mMonthFormat.format(calendar.getTime());
        init(calendar);
    }

    private void init(Calendar calendar) {
        mDateString = new ArrayList<>();
        for (ScheduleDate date : mDateList) {
            mDateString.add(date.getDate());
        }
        int daysOfMonth = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH);
        int day = calendar.get(Calendar.DAY_OF_MONTH);
        calendar.add(Calendar.MONTH, -1);// 减一个月
        calendar.set(Calendar.DATE, 1);// 把日期设置为当月第一天
        calendar.roll(Calendar.DATE, -1);// 日期回滚一天，也就是本月最后一天
        int DAY_OF_WEEK = calendar.get(Calendar.DAY_OF_WEEK);
        if (DAY_OF_WEEK == 7) {
            DAY_OF_WEEK = 0;
        } else {
            calendar.add(Calendar.DAY_OF_MONTH, -DAY_OF_WEEK);
        }
        int countNumber = daysOfMonth + DAY_OF_WEEK;

        mCount = countNumber % 7 == 0 ? countNumber : countNumber + (7 - countNumber % 7);

        mDateModelArrays = new ArrayList<>();
        for (int i = 0; i < mCount; i++) {
            calendar.add(Calendar.DAY_OF_MONTH, 1);
            int monthState;
            if (i < DAY_OF_WEEK) {
                monthState = -1;
            } else if (i >= DAY_OF_WEEK + daysOfMonth) {
                monthState = 1;
            } else {
                monthState = 0;
            }
            mDateModelArrays.add(new DateModel(calendar, monthState));
        }

        calendar.set(year, month, day);
    }

    public void refreshDate(Calendar calendar, ArrayList<ScheduleDate> dateList) {
        mDateList = dateList == null ? new ArrayList<ScheduleDate>() : dateList;
        init(calendar);
        notifyDataSetChanged();
    }

    public String getCurrentMonth() {
        return mCurrentMonth;
    }

    @Override
    public int getCount() {
        return mCount;
    }

    @Override
    public Object getItem(int position) {
        return mDateModelArrays.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.calendar_item, null);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        DateModel dateModel = mDateModelArrays.get(position);
        viewHolder.textTop.setText(dateModel.getStringDay());

        int monthResult = dateModel.getMonthState();
        switch (monthResult) {
            case -1://上月
            case 1://下月
                viewHolder.item.setBackgroundColor(mTransparent);
                viewHolder.textTop.setTextColor(Color.parseColor("#AAAAAA"));
                viewHolder.textBottom.setText("");
                break;
            case 0://当前月
                int dayResult = dateModel.getDate().compareTo(mCurrentDate);
                if (dayResult > 0) {
                    operate(viewHolder, dateModel);
                } else if (dayResult == 0) {
                    viewHolder.textTop.setTextColor(Color.parseColor("#E64B50"));
                    viewHolder.textBottom.setTextColor(Color.parseColor("#D04448"));
                    viewHolder.textBottom.setText("今天");
                    viewHolder.item.setBackgroundResource(R.drawable.date_current);
                } else {
                    close(viewHolder);
                }
                break;
        }
        return convertView;
    }

    private void close(ViewHolder viewHolder) {
        viewHolder.textTop.setTextColor(mBlack);
        viewHolder.item.setBackgroundColor(mTransparent);
        viewHolder.textBottom.setTextColor(mTransparent);
        viewHolder.textBottom.setText("");
    }

    private void operate(ViewHolder viewHolder, DateModel dateModel) {
        viewHolder.textTop.setTextColor(mWhite);
        viewHolder.textBottom.setTextColor(mWhite);
        if (mDateString.contains(dateModel.getDate())) {
            ScheduleDate schedule = mDateList.get(mDateString.indexOf(dateModel.getDate()));
            if (schedule.getNum() == -1) {
                viewHolder.textTop.setTextColor(mBlack);
                viewHolder.textBottom.setText("");
                viewHolder.item.setBackgroundResource(R.drawable.date_error);
            } else if (schedule.getNum() == 0) {
                viewHolder.textBottom.setText("已满");
                viewHolder.item.setBackgroundResource(R.drawable.date_default);
            } else {
                dateModel.setScheduleDate(schedule);
                viewHolder.textBottom.setText(schedule.getNum() + "");
                viewHolder.item.setBackgroundResource(R.drawable.date_selected);
            }
        } else {
            viewHolder.textTop.setTextColor(mBlack);
            viewHolder.textBottom.setText("");
            viewHolder.item.setBackgroundResource(R.drawable.date_error);
        }
    }

    private class ViewHolder {
        TextView textTop;
        TextView textBottom;
        LinearLayout item;

        public ViewHolder(View view) {
            textTop = (TextView) view.findViewById(R.id.text_top);
            textBottom = (TextView) view.findViewById(R.id.text_bottom);
            item = (LinearLayout) view.findViewById(R.id.item);
        }
    }
}
